package com.sgb.goods.view.adapter.publish;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsItemVideoBinding;
import com.sgb.goods.entity.VideoEntity;
import com.sgb.goods.utils.base.adapter.BaseCommonAdapter;
import com.swgk.core.util.MyMediaUtils;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;

public class VideoAdapter extends BaseCommonAdapter<VideoEntity> {

    /** 默认的添加视图item */
    private final VideoEntity defaultItem = new VideoEntity(true);
    /** 上传视频的最大张数 */
    private int maxSize = 5;
    public ISelectPop iSelectPop;

    public interface ISelectPop {
        void openPop(int index);
        void openVideo(VideoEntity url);
    }

    public void OnSelectPop(ISelectPop iSelectPop) {
        this.iSelectPop = iSelectPop;
    }

    public VideoAdapter(Context context, int maxSize) {
        super(R.layout.goods_item_video, BR.viewModel);
        this.maxSize = maxSize;
        super.addData(defaultItem);
    }

    @Override
    protected void convert(BaseViewHolder baseHolder, ViewDataBinding dataBinding, VideoEntity entity) {
        GoodsItemVideoBinding mDataBinding = (GoodsItemVideoBinding) dataBinding;
        // 视频缩略图设置
        if(null != entity.url) {
            if (entity.url.contains("http")) {
                //如果地址包含http，就用网络加载视频的方式
                Glide.with(mContext).load(entity.url).into(mDataBinding.first.photoImg);
                mDataBinding.first.time.setText(entity.time);
            } else {
                //没有则用本地加载视频的方式
                mDataBinding.first.photoImg.setImageBitmap(setVideoFirstImg(entity.url));
                mDataBinding.first.time.setText(MyMediaUtils.getPlayTime(entity.url));
            }
            //点击视频
            mDataBinding.first.photoImg.setOnClickListener(view -> openVideoView(baseHolder.getLayoutPosition()));
            //点击删除按钮
            mDataBinding.first.deleteImg.setOnClickListener(v -> {
                getData().remove(entity);
                notifyDataSetChanged();
            });
        }

        if(mDataBinding.second.getRoot().getVisibility() == View.VISIBLE) {
            mDataBinding.second.addRl.setOnClickListener(v -> iSelectPop.openPop(getData().size()));
            // 如果item个数==展示最大数量缩略图，那么隐藏添加视频的入口
            mDataBinding.second.getRoot().setVisibility(getItemCount() == maxSize + 1 ? View.GONE : View.VISIBLE);
        }
    }

    public int getDataSize() {
        return Math.max(getData().size() - 1, 0);
    }

    @Override
    public void addData(@NonNull @NotNull VideoEntity data) {
        sort();
        super.addData(data);
        super.addData(defaultItem);
    }

    @Override
    public void addData(@NonNull @NotNull Collection<? extends VideoEntity> newData) {
        sort();
        super.addData(newData);
        super.addData(defaultItem);
    }

    /** 默认的添加视图排序 */
    private void sort() {
        int index = getData().indexOf(defaultItem);
        if(-1 != index) {
            getData().remove(defaultItem);
            notifyItemChanged(index);
        }
    }

    /**
     * 预览  fixme: 此处需要调用app的公用视频播放，不进行迁移，最后验证
     **/
    private void openVideoView(int position) {
//        if (intent == null) {
//            intent = new Intent();
//        }
//        if (bundle == null)
//            bundle = new Bundle();
//        List<VideoBean> videoBeanList = new ArrayList<>();
//        for (int i = 0; i < urls.size(); i++) {
//            VideoBean videoBean = new VideoBean();
//            videoBean.setCoverURL(urls.get(i).getUrl());
//            videoBean.setVideoId(urls.get(i).getVideoId());
//            videoBean.setTitle(urls.get(i).getTitle());
//            videoBeanList.add(videoBean);
//        }
//        Intent intent = new Intent(mContext, PlayListActivity.class);
//        intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) (Serializable) videoBeanList);
//        intent.putExtra(PlayListActivity.PSO, position);
//        intent.putExtra(PlayListActivity.IS_ShOW_USER, false);
//        intent.putExtra(PlayListActivity.VIDEOTYPE, "fx_mine");
//        mContext.startActivity(intent);
    }

    //本地视频时长
    private Bitmap setVideoFirstImg(String url) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(url);
        Bitmap bitmap = media.getFrameAtTime();
        return bitmap;
    }
}